package br.ufrj.dcc.smarthotel.controls;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import br.ufrj.dcc.smarthotel.activities.AppActivity;
import br.ufrj.dcc.smarthotel.activities.EventDetailActivity;
import br.ufrj.dcc.smarthotel.entities.Event;
import br.ufrj.dcc.smarthotel.entities.EventItem;
import br.ufrj.dcc.smarthotel.entities.EventUser;
import br.ufrj.dcc.smarthotel.entities.User;

import com.google.gson.reflect.TypeToken;

public class EventDetailControl extends Control {
	private EventDetailActivity activity;
	
	public EventDetailControl(EventDetailActivity activity) {
		this.activity = activity;
	}

	@SuppressWarnings("unchecked")
	public void loadEventDetails(int clickedEventId) {
		Event event = (Event) this.retrieve(this.baseURI + "event/" + clickedEventId, Event.class);
		
		this.activity.setClickedEvent(event);
		
		Type listType = new TypeToken<List<EventUser>>() {}.getType();
		List<EventUser> attendantsList = (List<EventUser>) this.retrieve(this.baseURI + "event/" + event.getEventId() + "/user", listType);
		
		this.activity.setAttendantsList(attendantsList);
		
		listType = new TypeToken<List<EventItem>>() {}.getType();
		List<EventItem> itemsList = (List<EventItem>) this.retrieve(this.baseURI + "event/" + event.getEventId() + "/item", listType);
		
		this.activity.setItemsList(itemsList);
	}

	public void ConfirmUser() {
		Event event = this.activity.getClickedEvent();
		User user = AppActivity.getSessionInfo().getUser();
		
		Map<String, Object> parameters = new HashMap<String, Object>();
		
		parameters.put("user", user);
		parameters.put("isUserManager", false);
		parameters.put("isUserConfirmed", true);
		
		this.doPost("event/" + event.getEventId() + "/user", parameters);
	}

	public void UnconfirmUser() {
		Event event = this.activity.getClickedEvent();
		
		this.doDelete("event/" + event.getEventId() + "/user");
	}
}
